<?php

class seasons_model extends MY_Model
{

    function __construct()
    {
        parent::__construct();
        $this->table='seasons';

    }

    function find_all_periods($seasons_ID)
    {
        $q="
            SELECT * FROM periods
            WHERE seasons_ID =  $seasons_ID
            ";

        $res=$this->db->query($q)->result_array();
        return $res;

    }

    function delete_periods($seasons_ID)
    {
        $q="
            DELETE FROM periods
            WHERE seasons_ID =  $seasons_ID
            ";

        $res=$this->db->query($q);
        return $res;

    }

    function find_active_period($seasons_ID, $start)
    {
        // keriraj $start_next_year, da godina bude 1971
        $start_d_m=date('d.m', $start);
        $start_next_year=strtotime($start_d_m.'.1971');

        // ovdje provjeravamo opcije 1 i 3, takodje ovdje se provjerava i opcija 2
        $q="
            SELECT * FROM periods
            WHERE seasons_ID =  $seasons_ID
            AND
            (($start > `start` AND $start <= `end`) OR ($start_next_year > `start` AND $start_next_year <= `end`))           
            ";

        $res=$this->db->query($q)->result_array();

        if(count($res) == 1)
            return $res[0]['ID'];
        else
            // if season is default whole year season, start and end are 0, so 0 row will be found in query
            // we return 1 becouse we supose an active season is ID 1 -> Whole year
            return 1;

    }

    function find_active_period_again($seasons_ID, $start)
    {
        $q="
            SELECT * FROM periods
            WHERE seasons_ID =  $seasons_ID
            ";

        $res=$this->db->query($q)->result_array();

//            $this->firephp->log('*** Seasons Model ***');
//            $this->firephp->log(date('d.m.Y', $start));
//            $this->firephp->log($res);

        // no periods defined for seasion
        if(count($res) == 0):
            return 0;
        endif;

        // whole year period check
        if(count($res) == 1):
            if($res[0]['start'] == $res[0]['end']):
                return $res[0]['ID'];
            endif;
        endif;

        /*
         * pronalazi odgovarajuci period izmedju start i end perioda
         *
         * ovdje postoje 3 opcije, a samo dvije provjeravama, jer druga provjera ukljucuje opciju 2 i 3
         *
         * opcija 1: Ne postoje periodi u 1971. godini
         * opcija 2: Postoji samo jedan end period u 1971. godini
         * opcija 23: Postoji vise perioda start i end u 1971. godini
         *
         */

        // keriraj $start_next_year, da godina bude 1971
        $start_d_m=date('d.m', $start);
        $start_next_year=strtotime($start_d_m.'.1971');

        // ovdje provjeravamo opcije 1 i 3, takodje ovdje se provjerava i opcija 2
        $q="
            SELECT * FROM periods
            WHERE seasons_ID =  $seasons_ID
            AND
            (($start > `start` AND $start <= `end`) OR ($start_next_year > `start` AND $start_next_year <= `end`))           
            ";

        $res=$this->db->query($q)->result_array();

        if(count($res) == 1)
            return $res[0]['ID'];
        else
            return 0; // Hotel is closed

    }



    function get_periods_release_period($seasons_ID, $start)
    {

        $start_d_m=date('d.m', $start);
        $start1970 = strtotime($start_d_m.'.1970');
        $start_next_year=strtotime($start_d_m.'.1971');

        // ovdje provjeravamo opcije 1 i 3, takodje ovdje se provjerava i opcija 2
        $q="
            SELECT * FROM periods
            WHERE seasons_ID =  $seasons_ID
            AND
            (($start1970 > `start` AND $start1970 <= `end`) OR ($start_next_year > `start` AND $start_next_year <= `end`))
            ";

        /*
        $start_d_m=date('d.m', $start);
        $start1970 = strtotime($start_d_m.'.1970');

        $q="
        SELECT * FROM periods
        WHERE seasons_ID =  $seasons_ID
        AND
        ($start1970 > `start` AND $start1970 <= `end`)
        ";


*/
        $res=$this->db->query($q)->result_array();

        if(count($res) == 1)
            return $res[0]['release_period'];
        else
            // if season is default whole year season, start and end are 0, so 0 row will be found in query
            // we return 1 becouse we supose an active season is ID 1 -> Whole year
        {

            // TODO check what to do with this whole year season // best solution, take it in else
            // This will return whole year season release_period, if there is one
            $q="
                SELECT * FROM periods
                WHERE seasons_ID =  $seasons_ID
                ";

            $res=$this->db->query($q)->result_array();

            return $res[0]['release_period'];

        }
    }

}

?>